<template>
  <div>
    <h2>欢迎访问蜗牛养老后台管理系统</h2>
    <el-card>
      <!--搜索框11-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入模板名称" clearable v-model="name" @clear="search">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openAddContractForm">创建合同模板</el-button>
        </el-col>
      </el-row>
      <!--添加合同模板-->
      <el-dialog title="创建合同模板" :visible.sync="addConFormVisible" @close="closeAddConForm"
                 :close-on-click-modal="false">
        <el-form ref="addConForm" :rules="addConFormRules" :model="addConForm" label-width="150px" size="mini">
          <el-form-item label="名称" prop="contractname">
            <el-input v-model="addConForm.contractname"></el-input>
          </el-form-item>
          <el-form-item label="合同类型" prop="contracttypeId">
            <el-select v-model="addConForm.contracttypeId" placeholder="请选择合同类型">
              <el-option label="请选择" value=""></el-option>
              <el-option v-for="t in typeList" :label="t.contracttypename" :value="t.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="contractremark">
            <el-input v-model="addConForm.contractremark"></el-input>
          </el-form-item>

          <el-form-item label="Word模板" prop="contractFile">
            <template>
              <el-upload
                  class="upload-demo"
                  ref="upload"
                  action="http://localhost:8088/upload/upFile"
                  :on-success="handleSuccess"
                  :before-upload="beforeUpload"
                  :on-error="handleError"
                  :auto-upload="true"
                  :file-list="fileList"
                  multiple
                  :limit="1"
                  name="conFile"
                  :headers="myHeader"
              >
                <el-button size="small" type="primary" @click="upload">选择模板文件</el-button>
                <div slot="tip" class="el-upload__tip">只能上传DOCX文件</div>
              </el-upload>
            </template>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAddConForm">取 消</el-button>
          <el-button type="primary" @click="AddConFormOK">确 定</el-button>
        </div>
      </el-dialog>
      <!--主体数据-->
      <template>
        <el-table
            :data="contactList"
            style="width: 100%"
            :row-class-name="tableRowClassName"
        >
          <el-table-column
              label="编号"
              type="index"
              align="center"
          >
          </el-table-column>
          <el-table-column
              prop="contractname"
              label="名称"
              align="center">
          </el-table-column>
          <el-table-column
              prop="contracttype.contracttypename"
              label="合同类型"
              align="center">
          </el-table-column>
          <el-table-column
              prop="contractremark"
              label="备注"
              align="center">
          </el-table-column>
          <el-table-column
              label="状态"
              align="center"
          >
            <template slot-scope="scope">
              <span style="color: green" v-if="scope.row.contractstatus == 1">已启用</span>
              <span style="color: red" v-if="scope.row.contractstatus == 0">已停用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  @click="handleEdit(scope.row.id)">编辑
              </el-button>
              <el-button
                  size="mini"
                  type="warning"
                  v-if="scope.row.contractstatus == 1"
                  @click="handleGoBack(scope.row.id,0)">停用
              </el-button>
              <el-button
                  size="mini"
                  type="success"
                  v-if="scope.row.contractstatus == 0"
                  @click="handleGoBack(scope.row.id,1)">启用
              </el-button>
              <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!--分页-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6 , 12, 24]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
      <!--修改合同模板-->
      <el-dialog title="修改合同模板" :visible.sync="modifyConFormVisible" @close="closeModifyConForm"
                 :close-on-click-modal="false">
        <el-form ref="modifyConForm" :rules="modifyConFormRules" :model="modifyConForm" label-width="150px" size="mini">
          <el-form-item label="名称" prop="contractname">
            <el-input v-model="modifyConForm.contractname"></el-input>
          </el-form-item>
          <el-form-item label="合同类型" prop="contracttypeId">
            <el-select v-model="modifyConForm.contracttypeId">
              <el-option v-for="t in typeList" :label="t.contracttypename" :value="t.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="contractremark">
            <el-input v-model="modifyConForm.contractremark"></el-input>
          </el-form-item>
          <el-form-item label="模板是否上传" prop="contractFile">
            <template>
              <el-upload
                  class="upload-demo"
                  ref="EditUplod"
                  action="http://localhost:8088/upload/upFile"
                  :on-success="handleEditSuccess"
                  :before-upload="beforeEditUpload"
                  :on-error="handleEditError"
                  :auto-upload="true"
                  :file-list="fileList"
                  multiple
                  :limit="1"
                  name="conFile"
                  :headers="myHeader"
              >
                <el-button size="small" type="danger" v-if="modifyConForm.contractFile!=null" @click="Editupload">
                  已上传模板文件
                </el-button>
                <div slot="tip" class="el-upload__tip">只能上传DOCX文件</div>
              </el-upload>
            </template>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAddConForm">取 消</el-button>
          <el-button type="primary" @click="modifyConFormOK">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {toRefs} from "vue";

export default {
  data() {
    return {
      myHeader: {
        token: ''
      },
      fileList: [],
      typeList: [],
      addConForm: {},
      modifyConForm: {},
      contactList: [],
      modifyConFormVisible: false,
      addConFormVisible: false,
      name: '',
      pageNum: 1,
      pageSize: 3,
      total: 0,
      addConFormRules: {
        contractname: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        contracttypeId: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        contractFile: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
      },
      modifyConFormRules: {
        contractname: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        contracttypeId: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        contractFile: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    initHeaders() {
      let token = sessionStorage.getItem("token");
      this.myHeader.token = token;
    },
    //上传失败
    handleError(response, file, fileList) {
      this.$message({
        message: response.msg,
        type: 'error',
        duration: 1000
      });
    },
    //上传文件
    upload() {
      this.$refs.upload.submit();
    },
    //文件上传成功之后
    handleSuccess(response, file, fileList) {
      this.addConForm.contractFile = response.data;
      this.modifyConForm.contractFile = response.data;
    },
    //文件上传之前
    beforeUpload(file) {
      /* if (!file.type.endsWith('docx')) {
         alert('文件类型不是DOCX');
         return false; // 阻止文件上传
       }*/
    },
    //上传失败
    handleEditError(response, file, fileList) {
      this.$message({
        message: response.msg,
        type: 'error',
        duration: 1000
      });
    },
    //上传文件
    Editupload() {
      this.$refs.EditUplod.submit();
    },
    //文件上传成功之后
    handleEditSuccess(response, file, fileList) {
      this.addConForm.contractFile = response.data;
      this.modifyConForm.contractFile = response.data;
    },
    //文件上传之前
    beforeEditUpload(file) {
      /* if (!file.type.endsWith('docx')) {
         alert('文件类型不是DOCX');
         return false; // 阻止文件上传
       }*/
    },
    //打开添加合同模板对话框
    openAddContractForm() {
      this.addConFormVisible = true;
      this.addConForm = {};
    },
    tableRowClassName({rowIndex}) {
      if (rowIndex === 0) {
        return 'white-row'; // 第一行为白色
      } else if (rowIndex % 4 === 1) {
        return 'warning-row'; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 2 === 0) {
        return ''; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 3 === 2) {
        return 'white-row'; // 第三行和每隔三行后的行为白色
      } else {
        return 'success-row'; // 每隔三行后的行为黄色
      }
    },
    handleDelete(id) {
      this.$confirm("你确定要删除此记录吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let url = '/contract/deleteContract/' + id
        this.$axios.delete(url).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: res.data.msg,
              type: 'success',
              duration: 1000
            });
            this.initConList();
          } else {
            this.$message({
              message: res.data.msg,
              type: 'error',
              duration: 1000
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "取消成功"
        })
      })

    },
    closeAddConForm() {
      this.addConFormVisible = false;
    },
    //修改状态
    handleGoBack(id, status) {
      let url = '/contract/modifyContractStatus/' + id + '/' + status;
      this.$axios.put(url).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: res.data.msg,
            type: 'success',
            duration: 1000
          });
          this.initConList();
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //打开修改合同表
    handleEdit(id) {
      this.modifyConFormVisible = true;
      let url = "/contract/selectContract/" + id;
      this.$axios.get(url).then((res) => {
        if (res.data.code == 200) {
          this.modifyConForm = res.data.data;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //确认添加
    AddConFormOK() {
      if (this.addConForm.contractFile == null) {
        alert("请选择合同")
        return
      }
      this.$refs.addConForm.validate((vaild) => {
        if (!vaild) {
          return
        } else {
          this.$confirm("你确定要创建吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            let url = '/contract/addContract'
            this.$axios.post(url, this.addConForm).then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  message: res.data.msg,
                  type: 'success',
                  duration: 1000
                });
                this.initConList();
                this.addConFormVisible = false;
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error',
                  duration: 1000
                });
              }
            })
          }).catch(() => {
            this.$message({
              type: "info",
              message: "取消成功"
            })
          })
        }
      })
    },
    //确认修改
    modifyConFormOK() {
      this.$refs.modifyConForm.validate((vaild) => {
        if (!vaild) {
          alert("请先完善数据")
          return
        } else {
          this.$confirm("你确定要修改吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            let url = '/contract/modifyContract'
            this.$axios.put(url, this.modifyConForm).then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  message: res.data.msg,
                  type: 'success',
                  duration: 1000
                });
                this.initConList();
                this.modifyConFormVisible = false;
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error',
                  duration: 1000
                });
              }
            })
          }).catch(() => {
            this.$message({
              type: "info",
              message: "取消成功"
            })
          })
        }
      })
    },
    //关闭修改对话框
    closeModifyConForm() {
      this.modifyConFormVisible = false;
    },
    //查询合同
    search() {
      this.pageNum = 1;
      this.initConList();
    },
    //初始化合同类型信息
    initTypeList() {
      let url = "/contractType/list"
      this.$axios.get(url).then((res) => {
        if (res.data.code == 200) {
          this.typeList = res.data.data;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //初始化查询合同模板数据
    initConList() {
      let url = "/contract/list"
      this.$axios.get(url, {
        params: {
          name: this.name,
          now: this.pageNum,
          size: this.pageSize
        }
      }).then((res) => {
        if (res.data.code == 200) {
          this.contactList = res.data.data.list;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //改变条数查询
    handleSizeChange(size) {
      this.pageSize = size;
      this.pageNum = 1;
      this.initConList();
    },
    //改变页数查询
    handleCurrentChange(now) {
      this.pageNum = now;
      this.initConList();
    },
  },
  created() {
    this.initConList();
    this.initTypeList();
    this.initHeaders();
  }
}
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

</style>